package com.ys.otms.utils.ali;
import com.aliyun.mns.client.CloudAccount;
import com.aliyun.mns.client.CloudQueue;
import com.aliyun.mns.client.MNSClient;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.ys.otms.utils.ali.QueryTokenForMnsQueueResponse.MessageTokenDTO;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TokenGetterForAlicom {
    private Log logger = LogFactory.getLog(TokenGetterForAlicom.class);
    private String accessKeyId;
    private String accessKeySecret;
    private String endpointNameForPop;
    private String regionIdForPop;
    private String domainForPop;
    private IAcsClient iAcsClient;
    private static final String productName = "Dybaseapi";
    private long bufferTime = 120000L;
    private final Object lock = new Object();
    private ConcurrentMap<String, TokenForAlicom> tokenMap = new ConcurrentHashMap();

    public TokenGetterForAlicom(String accessKeyId, String accessKeySecret, String endpointNameForPop, String regionIdForPop, String domainForPop) throws ClientException {
        this.accessKeyId = accessKeyId;
        this.accessKeySecret = accessKeySecret;
        this.endpointNameForPop = endpointNameForPop;
        this.regionIdForPop = regionIdForPop;
        this.domainForPop = domainForPop;
        this.init();
    }

    private void init() throws ClientException {
        DefaultProfile.addEndpoint(this.endpointNameForPop, this.regionIdForPop, "Dybaseapi", this.domainForPop);
        IClientProfile profile = DefaultProfile.getProfile(this.regionIdForPop, this.accessKeyId, this.accessKeySecret);
        this.iAcsClient = new DefaultAcsClient(profile);
    }

    private TokenForAlicom getTokenFromRemote(String messageType) throws ServerException, ClientException, ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryTokenForMnsQueueRequest request = new QueryTokenForMnsQueueRequest();
        request.setAcceptFormat(FormatType.JSON);
        request.setMessageType(messageType);
        request.setProtocol(ProtocolType.HTTPS);
        QueryTokenForMnsQueueResponse response = (QueryTokenForMnsQueueResponse)this.iAcsClient.getAcsResponse(request);
        String resultCode = response.getCode();
        if (resultCode != null && "OK".equals(resultCode)) {
            MessageTokenDTO dto = response.getMessageTokenDTO();
            TokenForAlicom token = new TokenForAlicom();
            String timeStr = dto.getExpireTime();
            token.setMessageType(messageType);
            token.setExpireTime(df.parse(timeStr).getTime());
            token.setToken(dto.getSecurityToken());
            token.setTempAccessKeyId(dto.getAccessKeyId());
            token.setTempAccessKeySecret(dto.getAccessKeySecret());
            return token;
        } else {
            this.logger.error("getTokenFromRemote_error,messageType:" + messageType + ",code:" + response.getCode() + ",message:" + response.getMessage());
            throw new ServerException(response.getCode(), response.getMessage());
        }
    }

    public TokenForAlicom getTokenByMessageType(String messageType, String queueName, String mnsAccountEndpoint) throws ServerException, ClientException, ParseException {
        TokenForAlicom token = (TokenForAlicom)this.tokenMap.get(messageType);
        Long now = System.currentTimeMillis();
        if (token == null || token.getExpireTime() - now < this.bufferTime) {
            Object var6 = this.lock;
            synchronized(this.lock) {
                token = (TokenForAlicom)this.tokenMap.get(messageType);
                if (token == null || token.getExpireTime() - now < this.bufferTime) {
                    TokenForAlicom oldToken = null;
                    if (token != null) {
                        oldToken = token;
                    }

                    token = this.getTokenFromRemote(messageType);
                    CloudAccount account = new CloudAccount(token.getTempAccessKeyId(), token.getTempAccessKeySecret(), mnsAccountEndpoint, token.getToken());
                    MNSClient client = account.getMNSClient();
                    CloudQueue queue = client.getQueueRef(queueName);
                    token.setClient(client);
                    token.setQueue(queue);
                    this.tokenMap.put(messageType, token);
                    if (oldToken != null) {
                        oldToken.closeClient();
                    }
                }
            }
        }

        return token;
    }
}
